package study.jdbc;

import java.sql.*;

/**
 * @author Abner
 * @date 2021/10/25 0:52
 * @email songkd90@163.com
 * @description JDBC操作
 */
public class NomalJDBC {
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123456";
    private static final String URL = "jdbc:mysql://localhost:3306/testDB";

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName(DRIVER_NAME);
            connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            statement = connection.createStatement();
            // 插入数据
            insertData(statement);
            // 查询数据
            queryData(statement);
            // 更新数据
            updateData(statement);
            // 删除数据
            deleteData(statement);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 插入数据
     * @param statement
     */
    private static void insertData(Statement statement) {
        String insertSql = "insert into test_table(name, age) values ('张三', 28)";
        int resultSet;
        try {
            resultSet = statement.executeUpdate(insertSql);
            System.out.println("成功写入" + resultSet +"条数据");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 查询数据
     * @param statement
     */
    private static void queryData(Statement statement) {
        String querySql = "select name, age from test_table";
        ResultSet resultSet = null;
        try {
            resultSet = statement.executeQuery(querySql);
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                System.out.println("name: " + name + ", age: " + age);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 更新数据
     * @param statement
     */
    private static void updateData(Statement statement) {
        String updateSql = "update test_table set name = '李四', age = 29";
        int resultSet;
        try {
            resultSet = statement.executeUpdate(updateSql);
            System.out.println("成功更新" + resultSet +"条数据");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 删除数据
     * @param statement
     */
    private static void deleteData(Statement statement) {
        String deleteSql = "delete from test_table where name = '李四'";
        int result;
        try {
            result = statement.executeUpdate(deleteSql);
            System.out.println("成功删除" + result + "条数据");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
